#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>


typedef struct {
    int* a;
    int tail;
    int head;
    int k;


} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) {
    MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    if (obj == NULL)
    {
        perror("malloc fail");
    }
    int* tmp = (int*)malloc(sizeof(int) * (k + 1));
    if (tmp == NULL)
    {
        perror("malloc fail");
    }
    obj->a = tmp;
    obj->tail = obj->head = 0;
    obj->k = k;
    return obj;

}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    return obj->head == obj->tail;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
    int next = obj->tail + 1;
    if (next == obj->k + 1)

        next = 0;



    return next == obj->head;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if (myCircularQueueIsFull(obj))
        return false;
  
    obj->a[obj->tail] = value;
    obj->tail++;
    if (obj->tail== obj->k + 1)
    {
        obj->tail = 0;
    }
    return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
        return false;
   
    obj->head++;
    if (obj->head== obj->k + 1)
    {
        obj->head = 0;
    }

    return true;
}

int myCircularQueueFront(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
        return -1;
    return obj->a[obj->head];

}

int myCircularQueueRear(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
        return -1;

    int front = obj->tail - 1;
    if (front == -1)
    {
        front = obj->k;
    }
    return obj->a[front];

}



void myCircularQueueFree(MyCircularQueue* obj) {
    free(obj->a);
    free(obj);

}
int main()
{
    MyCircularQueue*st=myCircularQueueCreate(3);

  int r1=  myCircularQueueEnQueue(st, 1);
  int r2=  myCircularQueueEnQueue(st, 2);
  int r3 = myCircularQueueDeQueue(st);
  int r4=  myCircularQueueEnQueue(st, 3);
  int r5 = myCircularQueueDeQueue(st);

  int r6=  myCircularQueueEnQueue(st, 4);
  int r7 = myCircularQueueDeQueue(st);
  int r8 = myCircularQueueEnQueue(st, 4);
  int r9 = myCircularQueueDeQueue(st);
  int r10 = myCircularQueueFront(st);


  //int r5=  myCircularQueueRear(st);
  //int r6=  myCircularQueueIsFull(st);
  //int r7= myCircularQueueDeQueue(st);
  //int r8= myCircularQueueEnQueue(st, 4);
  //int r9= myCircularQueueRear(st);
  printf("%d %d %d %d %d %d %d %d %d %d", r1, r2, r3, r4, r5, r6, r7, r8, r9,r10);



}